package dressSmart.modules.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import dressSmart.modules.mapper.TagClothesMapper;
import dressSmart.modules.mapper.UserClothesMapper;
import dressSmart.modules.model.Clothes;
import dressSmart.modules.mapper.ClothesMapper;
import dressSmart.modules.model.TagClothes;
import dressSmart.modules.model.UserClothes;
import dressSmart.modules.service.ClothesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import dressSmart.modules.service.TagClothesService;
import dressSmart.modules.service.UserClothesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author znx
 * @since 2022-11-11
 */
@Service
public class ClothesServiceImpl extends ServiceImpl<ClothesMapper, Clothes> implements ClothesService {

    @Autowired
    private TagClothesService tagClothesService;

    @Autowired
    private UserClothesService userClothesService;



    @Override
    public List<Clothes> getList(Integer tagId, Integer userId,Integer timeSort) {

        LambdaQueryWrapper<TagClothes> tagQueryWrapper=new LambdaQueryWrapper<>();
        LambdaQueryWrapper<UserClothes> userQueryWrapper=new LambdaQueryWrapper<>();
        List<Integer> ClothesIdList = new ArrayList<>();
        /**
         * 从标签中查询 到 对应的衣服id
         */
        if (tagId!=null) {
            tagQueryWrapper.eq(TagClothes::getTagId,tagId);
            List<TagClothes> tagClothes = tagClothesService.list(tagQueryWrapper);
            for(TagClothes tag:tagClothes){
                ClothesIdList.add(tag.getClothesId());
            }
        }

        if(userId!=null){
            userQueryWrapper.eq(UserClothes::getUserId,userId);
            List<UserClothes> userClothes=userClothesService.list(userQueryWrapper);
            for (UserClothes user:userClothes){
                ClothesIdList.add(user.getClothesId());
            }
        }

        LambdaQueryWrapper<Clothes> clothesQueryWrapper=new LambdaQueryWrapper<>();
        if(ClothesIdList!=null&&ClothesIdList.size()!=0){
            clothesQueryWrapper.in(Clothes::getClothesId, ClothesIdList);

        }
        if(timeSort==1){
            clothesQueryWrapper.orderByAsc(Clothes::getUpdateTime);
        }else{
            clothesQueryWrapper.orderByDesc(Clothes::getUpdateTime);
        }

        return list(clothesQueryWrapper);
    }
}
